﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblItemDetailDAO
    {
         private DBService _db = null;
          public TblItemDetailDAO (string ConnectionString)
         {
            _db = new DBService(ConnectionString);
         }

          public bool UpdateVote(int itemID, int vote)
          {
              bool flag = false;
              try
              {
                  int result = 0;
                  result = _db.ExeSP_NonQuery("sp_TblItemDetail_Update ",
                      new string[] { "@itemID", "@Vote" },
                      new object[] { itemID, vote}
                      , CommandType.StoredProcedure);
                  flag = true;
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }
          public bool Insert_Update(TblItemDetail itemDetail, int mode)
          {
              bool flag = false;
              try
              {
                  int result = 0;
                  result = _db.ExeSP_NonQuery("sp_UpdateDescriptonItem",
                      new string[] { "@ItemDetailsID", "@ItemID", "@TinhNangNoiBat","@GiaiTri", "@Mode" },
                      new object[] { itemDetail.ItemDetailsID, itemDetail.ItemID, itemDetail.TinhNangNoiBat, itemDetail.GiaiTri, mode },
                      CommandType.StoredProcedure);

                  flag = true;
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }

          public bool UpdateItemDetails(int ItemDetailsID, int ItemID, string Details, string Content)
          {
              bool flag = false;
              try
              {
                  _db.ExeSP_NonQuery("Sp_TblItems_Details",
                      new string[] { "@ItemDetailsID", "@ItemID", "@Details", "@Content" },
                      new object[] { ItemDetailsID, ItemID, Details, Content},
                      CommandType.StoredProcedure);
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return flag;
          }


          public TblItemDetail GetEdit(int ItemDetailsID, int ItemID)
          {
              TblItemDetail item = new TblItemDetail();
              try 
              {
                 
                   DataTable dt=_db.ExeSP_DataTable ("sp_ItemDetailGet",
                       new string [] {"@ItemDetailsID", "@ItemID"},
                       new object [] {ItemDetailsID, ItemID},
                    CommandType.StoredProcedure);

                   foreach (DataRow dr in dt.Rows)
                 {
                    item.ItemDetailsID = Convert.ToInt32(dr["ItemDetailsID"].ToString());
                    item.ItemID = Convert.ToInt32(dr["ItemID"].ToString());
                    item.TinhNangNoiBat = dr["TinhNangNoiBat"].ToString();
                    item.GiaiTri = dr["GiaiTri"].ToString();
                    item.Nguon = dr["Nguon"].ToString();
                    item.NhacChuong = dr["NhacChuong"].ToString();
                    item.ThongTinChung = dr["ThongTinChung"].ToString();
                    item.UngDungvaTroChoi = dr["UngDungvaTroChoi"].ToString();
                    item.KetNoiDuLieu = dr["KetNoiDuLieu"].ToString();
                    item.BoNho = dr["BoNho"].ToString();
                    item.DanhBaTinNhanEmail = dr["DanhBaTinNhanEmail"].ToString();                  
                 }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return item;
          }
          
          public DataTable GetItemDetail(int ItemID)
          {
              DataTable dt = null;
              try
              {
                  dt = _db.ExeSP_DataTable("select * from TblItemDetail where ItemID = @ItemID",
                      new string[] { "@ItemID"},
                      new object[] {ItemID}
                      , CommandType.Text);
              }
              catch ( Exception ex)
              {
                  throw ex;
              }
              return dt;

          }
          public TblItemDetail Get(int ItemID)
          {
              TblItemDetail itemDetail = null;
              try
              {
                  DataTable dt = _db.ExeSP_DataTable("SELECT ItemID, TinhNangNoiBat, GiaiTri, UngDungvaTroChoi, NhacChuong, BoNho, DanhBaTinNhanEmail, KetNoiDuLieu, ThongTinChung, Nguon  FROM TblItemDetail WHERE ItemID = @ItemID",
                      new string[] { "@ItemID" }, new object[] { ItemID }, CommandType.Text);
                  foreach (DataRow dr in dt.Rows)
                  {
                      itemDetail = new TblItemDetail();

                      itemDetail.ItemID = Convert.ToInt32(dr["ItemID"].ToString());
                      itemDetail.TinhNangNoiBat = dr["TinhNangNoiBat"].ToString();
                      itemDetail.GiaiTri = dr["GiaiTri"].ToString();
                      itemDetail.UngDungvaTroChoi = dr["UngDungvaTroChoi"].ToString();
                      itemDetail.NhacChuong = dr["NhacChuong"].ToString();
                      itemDetail.BoNho = dr["BoNho"].ToString();
                      itemDetail.DanhBaTinNhanEmail = dr["DanhBaTinNhanEmail"].ToString();
                      itemDetail.KetNoiDuLieu = dr["KetNoiDuLieu"].ToString();
                      itemDetail.ThongTinChung = dr["ThongTinChung"].ToString();
                      itemDetail.Nguon = dr["Nguon"].ToString();

                  }
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return itemDetail;
          }

          public DataTable GetDetailsItems(int ItemID)
          {
              DataTable dt = null;
              try
              {
                  dt = _db.ExeSP_DataTable("select ItemID, TinhNangNoiBat, GiaiTri, UngDungvaTroChoi, NhacChuong, BoNho, DanhBaTinNhanEmail, KetNoiDuLieu, ThongTinChung, Nguon from TblItemDetail where ItemID = @ItemID ", 
                      new string[] {"@ItemID"},
                      new object[] {ItemID},
                      CommandType.Text);
              }
              catch (Exception ex)
              {
                  throw ex;
              }
              return dt;
          }

    }
}
